package com.wanghaixin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanghaixin.dao.TagMapper;
import com.wanghaixin.dto.DataGridResult;
import com.wanghaixin.dto.QueryDTO;
import com.wanghaixin.pojo.Tag;
import com.wanghaixin.pojo.TagExample;
import com.wanghaixin.service.TagService;
import com.wanghaixin.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: 王海新
 * @Date: 2020/10/29 08:21
 * @Description:
 */
@Service
public class TageServiceImp  implements TagService {

    @Autowired
    private TagMapper tagMapper;

    @Override
    public int addTag(Tag tag) {
        int i = tagMapper.insertSelective(tag);
        return i;
    }

    @Override
    public int deleteTag(Integer id) {
        int i = tagMapper.deleteByPrimaryKey(id);
        return i;
    }

    @Override
    public int updateTag(Tag tag) {
        int update = tagMapper.updateByPrimaryKeySelective(tag);
        return update;
    }

    @Override
    public Tag findById(Integer id) {
        Tag tag = tagMapper.selectByPrimaryKey(id);
        return tag;
    }

    @Override
    public DataGridResult findByPage(QueryDTO queryDTO) {
        //以PageHelper的形式拿到分页信息
        PageHelper.offsetPage(queryDTO.getOffset(),queryDTO.getLimit());

        //新建TagExample对象，如果传递了Example，就利用Example的排序能力
        TagExample example = new TagExample();
        //先来判断传递的queryDTO对象排序字段是否设置
        if (queryDTO.getSort() != null && !queryDTO.getSort().equals("")) {
        example.setOrderByClause("id" + queryDTO.getOrder());
        }
        List<Tag> tags = tagMapper.selectByExample(example);

        //封装数据
        PageInfo<Tag> info = new PageInfo<>(tags);
        long total = info.getTotal();
        DataGridResult dataGridResult = new DataGridResult(total,tags);
        return dataGridResult;
    }

    @Override
    public R findLineData() {

        //2.利用list集合拿到封装数据，符合页面格式
        List<String> xAxisData = new ArrayList<>();
        List yAxisData = new ArrayList();
        //1.获取数据
        List<Tag> tags = tagMapper.selectByExample(null);
        //迭代数据，将数据封装
        for (Tag tag : tags) {
            String name = tag.getName(); //获取名字
            xAxisData.add(name); //封装名字
            Long clickCount = tag.getClickCount(); //获取标签的点击量
            yAxisData.add(clickCount); //封装点击量
        }
        //根据页面line.html，返回是参数值，将数据返回
        return R.ok().put("xAxis",xAxisData).put("seriesData",yAxisData);
    }

    @Override
    public R findBarData() {
        List xAxisData = new ArrayList();
        List yAxis = new ArrayList();
        List<Tag> tags = tagMapper.selectByExample(null);
        for (Tag tag : tags) {
            xAxisData.add(tag.getName());
            yAxis.add(tag.getClickCount()); //获取点击量
        }
        return R.ok().put("xAxis",xAxisData).put("seriesData",yAxis);
    }

    @Override
    public R findPieData() {
        //通过去官网查看饼状图的数据封装结构，我们将数据封装如下
        List<String> legendData = new ArrayList<>();
        //数据结构是集合中嵌套map
        List<Map<String,Object>> seriesData = new ArrayList<>();
        List<Tag> tags = tagMapper.selectByExample(null);
        for (Tag tag : tags) {
            String name = tag.getName();
            Long clickCount = tag.getClickCount();
           legendData.add(name);
            HashMap<String, Object> map = new HashMap<>();
            map.put("name",name);
            map.put("value",clickCount);
            seriesData.add(map);
        }
        return R.ok().put("legendData",legendData).put("seriesData",seriesData);
    }


}
